<?php
namespace app\common\model;
use app\common\library\token\driver\Redis;
use think\Model;
use think\Db;
class Follow extends Model
{
    protected $name = 'follow_fan';
    private $redis;
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    public function __construct()
    {
        $this->redis = new \Redis();
        $this->redis->connect('127.0.0.1', 6379);
    }

    //增加关注
    public function _addFollow($data)
    {
        $fan_id = $data['fan_id'];       //粉丝id
        $follow_id = $data['follow_id']; //被关注人id
        if($follow_id==$fan_id ) return false;
        Db::startTrans(); //开启事务
        try{
            if($this->redis->zScore($follow_id.'_MyFollow',$fan_id)){ //对方是否也关注了我
                //相互关注
                Follow::update(['status'=>2],['fan_id'=>$follow_id,'follow_id'=>$fan_id]);//改为互关状态
                //redis插入关注列,粉丝列
                $this->redis->zAdd($fan_id.'_MyFollow',time(),$follow_id);
                $this->redis->zAdd($follow_id.'_MyFan',time(),$fan_id);
            }else{
                //单方面关注
                if(!$this->where(['follow_id'=>$follow_id,'fan_id'=>$fan_id])->find()) {
                    $this->save(['follow_id' => $follow_id, 'fan_id' => $fan_id]);//mysql插入
                }
                //redis插入关注列,粉丝列
                $this->redis->zAdd($fan_id.'_MyFollow',time(),$follow_id);//id,关注时间,被关注人id
                $this->redis->zAdd($follow_id.'_MyFan',time(),$fan_id);//被关注人id,关注时间,id

               
            }
            Db::commit();
            return true;
        }catch (\Exception $e){
            Db::rollback();
            return false;
        }
    }

    //取消关注
    public function _deleteFollow($data)
    {
        $fan_id = intval($data['fan_id']);       //粉丝id
        $follow_id = intval($data['follow_id']); //被关注人id
        if($follow_id==$fan_id ) return false;
        Db::startTrans(); //开启事务
        try{
            if($this->redis->zScore($follow_id.'_MyFollow',$fan_id)){ //对方是否也关注了我
                //已经相互关注
//                $where1 = [ ['fan_id','eq',$follow_id], ['follow_id','eq',$fan_id] ]; //or 因为mysql存储两种可能
//                $where2 = [  ['fan_id','eq',$fan_id], ['follow_id','eq',$follow_id] ];
                ; //改为单方面关注
//                $this->whereOr([$where1,$where2])->update(['status'=>1]); //改为单方面关注
                $where2['fan_id']=$fan_id;
                $where2['follow_id']=$follow_id;
                $where1['fan_id']=$follow_id;
                $where1['follow_id']=$fan_id;
                $this->whereOr(function ($query)use($where1,$where2) {
                    $query->where($where2)->whereOr($where1);
                 })->update(['status'=>1]); //改为单方面关注
                //redis删除关注列,粉丝列
                $this->redis->zRem($fan_id.'_MyFollow',$follow_id);
                $this->redis->zRem($follow_id.'_MyFan',$fan_id);
            }else{
                //取关
                $this->where(['follow_id'=>$follow_id,'fan_id'=>$fan_id])->delete();//mysql删除
                //redis删除关注列,粉丝列
                $this->redis->zRem($fan_id.'_MyFollow',$follow_id);
                $this->redis->zRem($follow_id.'_MyFan',$fan_id);
            }
            Db::commit();
            return true;
        }catch (Exception $e){
            Db::rollback();
            return false;
        }
    }

    //关注查询
    public function _followList($data)
    {
        $res = self::alias('a')
            ->rightJoin($data['join'],$data['condition'])
            ->where($data['where'])
            ->field($data['field'])
            ->order($data['order'])
            ->selectOrFail();
        return $res;
    }

}